<%-- 
    Document   : createProduct
    Created on : 02 2, 12, 6:27:07 PM
    Author     : pauld
--%>

<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="Controller.ProductManager"%>
<%@page import="Controller.UserManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <%
        String user = (String)session.getAttribute("user");
        String name = (String)session.getAttribute("name");
        
        //if the user is not logged in or is not an admin
        if(user == null || UserManager.isProductManager(user) == 0) {
            response.sendRedirect("../index.jsp");
        }
        //if the user is logged and is a product manager in check for session integrity
        else if( (String)session.getAttribute("user") != null && UserManager.isProductManager(user) != 0 ){
        
            //check session integrity by comparing the hash of the session token
            String currSession = request.getRemoteAddr().concat( session.getId() );
            try{
                if( !UserManager.checkSessionIntegrity(currSession, session) ){
                    UserManager.logout(request);
                    response.sendRedirect("../login.jsp");
                }
            }catch(NoSuchAlgorithmException e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
            catch(UnsupportedEncodingException e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
            catch(Exception e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
        
    }
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <link rel="stylesheet" href="../css/960_24_col.css" />
        <link rel="stylesheet" href="../css/reset.css" />
        <link rel="stylesheet" href="../css/text.css" />
        <link rel="stylesheet" href="../css/style.css" />
        
        <script type="text/javascript" src="../scripts/jquery.js"></script>
        <script type="text/javascript" src="../scripts/jquery-ui-1.8.18.custom.min.js"></script>
        <script type="text/javascript" src="../scripts/formScripts.js"></script>
        <script>
	$(function() {
		$( "#accordion" ).accordion();
	});
	</script>
        
        <title>FooBar Online Bookstore | Create Product</title>
    </head>
    <body>
        <div id="bar"></div>
        <!-------------------------------header here----------------------->
            <div id="header-wrap">
                <div id="dashboard" class="container_24">
                    <div id="dashboard-logo" class="grid_7">
                        <a href="../index.jsp"><img src="../images/logo.png" alt="logo" /></a>
                    </div>
                    <div id="dashboard-menu" class="grid_17">
                        <ul class="menu" id="main-menu">
                            <li><a href="../index.jsp">Home</a></li>
                            <li><a href="../main.jsp">Products</a>
                                <ul style="display: none;">
                                    <li><a href="../main.jsp?show=book">Books</a></li>
                                    <li><a href="../main.jsp?show=mags">Magazines</a></li>
                                    <li><a href="../main.jsp?show=cd">CD</a></li>
                                    <li><a href="../main.jsp?show=dvd">DVD</a></li>
                                    <li><a href="../main.jsp?show=acc">Accessories</a></li>
                                </ul>
                            </li>
                            <li><a href='productPanel.jsp'>Product Panel</a></li>
                            <li><a href="../about.jsp">About</a></li>
                            <li><a href="../contact.jsp">Contact Us</a></li>
                            <%
                            if(user == null){
                                out.println("<li><a href='../login.jsp'>Login</a> | <a href='register.jsp'>Join Now</a></li>");
                            }
                            else{
                                out.println("<li><form action='../logout' name='logout' method='post'>"
                                + "<a id='user-header' href='myProfile.jsp'>" + name + "</a>"
                                + "<input type='submit' value='Logout' class='submit-button' />"
                                + "</form></li>");
                            }
                            %>
                        </ul>
                    </div>
                </div>
            </div>
        <!-------------------------------content here----------------------->
        <div id="content-wrap">
            <div id="content" class="container_24">
                <h1>Product Manager Panel</h1>
                <div id="accordion">
                    <h3><a href="#">Create Product</a></h3>
                    <div>
                        <ul id="create-product-interface">
                            <li>
                                <form action="../CreateProduct" method="post" id="create-product-form" class="styled-form">
                                    <h1>Create Product</h1>
                                    <label>Product Name: </label><br />
                                    <input type="text" id="product-name" name="name" class="form-inputs" maxlength="45"
                                           value="<% if( session.getAttribute("product-name") != null )out.println(session.getAttribute("product-name")); %>"/><br />
                                
                                    <label>Synopsis: </label><br />
                                    <textarea name="synopsis" id="product-synopsis" cols="40" rows="3" class="form-inputs"><% if( session.getAttribute("product-synopsis") != null )out.println(session.getAttribute("product-synopsis")); %></textarea><br />
                                
                                    <label>Genre: </label><br />
                                    <select name="genre" id="product-genre">
                                        <option value="">----------------</option>
                                        <%
                                        //get all product genre
                                        ResultSet genres = ProductManager.getProductGenres();
                                
                                        while( genres.next() ){
                                            out.println("<option value='" + genres.getString(1) + "'>" + genres.getString(1) + "</option>");
                                        }
                                        %>
                                    </select><br />
                                
                                    <!--Query all the product types then display as options-->
                                    <label>Type: </label><br />
                                    <select name="type" id="product-type">
                                        <option value="">----------------</option>
                                        <%
                                        //get all product types
                                        ResultSet types = ProductManager.getProductTypes();
                                
                                        while( types.next() ){
                                            out.println("<option value='" + types.getString(1) + "'>" + types.getString(1) + "</option>");
                                        }
                                        %>
                                    </select><br />
                                
                                    <label>Price: </label><br />
                                    <input type="text" id="product-price" name="price" class="form-inputs"
                                           value="<% if( session.getAttribute("product-price") != null )out.println(session.getAttribute("product-price")); %>"/><br />
                                
                                    <input type="submit" value="Create Product" class="submit-button" />
                                </form>
                            </li>
                            <li>
                                <ul class="error-pane">
                                    <%
                                    //error pane
                                    try{
                                        if( request.getParameter("status").equals("1") ){
                                            out.println("<li class='important success'>Product Added</li>");
                                        }
                                        else if( request.getParameter("status").equals("0") ){
                                            out.println("<li class='important'>Fill Up The Form Correctly</li>");
                                        }
                                        else if( request.getParameter("status").equals("-1") ){
                                            out.println("<li class='important'>There is Already A Product With The Same Name and Type</li>");
                                        }
                                        else if( request.getParameter("status").equals("-2") ){
                                            out.println("<li class='important'>Oooops! Something Went Wrong Please Try Again Later</li>");
                                        }
                                    }
                                    catch(NullPointerException e1){}
                                    catch(Exception e1){}
                                    %>
                                    <li style="display: none;" id='product-panel-name'>please enter a valid product name (alphanumeric characters only)</li>
                                    <li style="display: none;" id='product-panel-synopsis'>the synopsis is too long</li>
                                    <li style="display: none;" id='product-panel-synopsis-none'>please enter a synopsis</li>
                                    <li style="display: none;" id='product-panel-genre'>please select a genre for your product</li>
                                    <li style="display: none;" id='product-panel-type'>please select the type of your product</li>
                                    <li style="display: none;" id='product-panel-price'>please enter a valid price</li>
                                </ul>
                            </li>
                        </ul>
                    </div>
                    
                    <h3><a href="#">Product List</a></h3>
                    <div>
                        <ul class="product-panel-list">
                            <%
                            //list all active products then print them out
                            ResultSet products = ProductManager.getAllProducts();

                            while( products.next() ){
                                if( products.getInt(7) == 1 ){
                                    out.println("<li><a href='editProduct.jsp?id=" + products.getString(8) + "'>" + products.getString(2) + "</a></li>");
                                }
                            }
                            %>
                        </ul>
                    </div>
                    
                    <h3><a href="#">Discontinued Product List</a></h3>
                    <div>
                        <ul class="product-panel-list">
                            <%
                            //list all inactive products and print them out
                            ResultSet discProducts = ProductManager.getAllProducts();

                            while( discProducts.next() ){
                                if( discProducts.getInt(7) == 0 ){
                                    out.println("<li><a href='editProduct.jsp?id=" + discProducts.getString(8) + "'>" + discProducts.getString(2) + "</a></li>");
                                }
                            }
                            %>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <!-------------------------------footer here----------------------->
        <div id="upper-footer-wrap">
            <div id="upper-footer" class="container_24">
                <div class="grid_7 prefix_17">
                    <ul class="menu">
                        <li><a href="#">top</a></li>
                        <li><a href="index.jsp">home</a></li>
                        <li><a href="contact.jsp">contact us</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div id="lower-footer-wrap">
            <div id="lower-footer" class="container_24">
                <center>
                    <p>FooBar Online Bookstore SECPROG Project</p>
                </center>
            </div>
        </div>
    </body>
</html>
